package pt.ua.helpdesk.client;

import java.util.ArrayList;

import pt.ua.helpdesk.client.PerformanceAss.fillPerformGridAsyncCallback;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class PerformanceGeral extends Composite {

	public PerformanceGeral() 
	{
		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		initWidget(horizontalPanel);

		final VerticalPanel verticalPanel = new VerticalPanel();
		horizontalPanel.add(verticalPanel);

		final Grid grid = new Grid();
		verticalPanel.add(grid);
		grid.resize(7, 2);
		grid.setStyleName("grid");
		grid.setBorderWidth(1);
		grid.setCellSpacing(4);

		
		Label lbl1 = new Label();
		lbl1.setText("Medidas de Performance");
		lbl1.setStyleName("section_title");
		grid.setWidget(0, 0, lbl1);
		
		Label lbl2 = new Label();
		lbl2.setText("Resultados de");
		lbl2.setStyleName("section_title");
		grid.setWidget(0, 1, lbl2);

		Label lbl3 = new Label();
		lbl3.setText("Nº pedidos efectuados");
		lbl3.setStyleName("label");
		grid.setWidget(1, 0, lbl3);
		
		Label lbl4 = new Label();
		lbl4.setText("Nº pedidos pendentes");
		lbl4.setStyleName("label");
		grid.setWidget(2, 0, lbl4);
		
		Label lbl5 = new Label();
		lbl5.setText("Nº pedidos resolvidos");
		lbl5.setStyleName("label");
		grid.setWidget(3, 0, lbl5);
		
		Label lbl6 = new Label();
		lbl6.setText("Nº pedidos em resolução");
		lbl6.setStyleName("label");
		grid.setWidget(4, 0, lbl6);
		
		Label lbl7 = new Label();
		lbl7.setText("Nº utilizadores");
		lbl7.setStyleName("label");
		grid.setWidget(5, 0, lbl7);
		
		Label lbl8 = new Label();
		lbl8.setText("Tempo médio de resolução");
		lbl8.setStyleName("label");
		grid.setWidget(6, 0, lbl8);
		
		ServicePerformance.Util.getInstance().getOverAllPerformance( new fillOverAllPerformAsyncCallback(grid));
	}

	static class fillOverAllPerformAsyncCallback implements AsyncCallback 
	{
		  Grid grid;
		
		  public fillOverAllPerformAsyncCallback(Grid grid)
		  {
			  this.grid= grid;
		
		  }
		  
		  /**
		   * Success handler
		   */
		  public void onSuccess(Object object) {
			  ArrayList perform = (ArrayList) object;
			  
			  Integer done = new Integer(perform.get(0).toString());
			  Label lbldone = new Label();
			  lbldone.setText(done.toString());
			  lbldone.setStyleName("label");
			  grid.setWidget(1, 1, lbldone); 			  
			  
			  Integer pendent = new Integer(perform.get(1).toString());
			  Label lblpendent = new Label();
			  lblpendent.setText(pendent.toString());
			  lblpendent.setStyleName("label");
			  grid.setWidget(2, 1, lblpendent);
			  
			  Integer solved = new Integer(perform.get(2).toString());
			  Label lblsolved = new Label();
			  lblsolved.setText(solved.toString());
			  lblsolved.setStyleName("label");
			  grid.setWidget(3, 1, lblsolved);
			  
			  Integer solving = new Integer(done.intValue() - pendent.intValue() - solved.intValue());
			  Label lblsolving = new Label();
			  lblsolving.setText(solving.toString());
			  lblsolving.setStyleName("label");
			  grid.setWidget(4, 1, lblsolving);
			  
			  Label lbluser = new Label();
			  lbluser.setText(perform.get(4).toString()); //XXX não vai ter a mesma ordem
			  lbluser.setStyleName("label");
			  grid.setWidget(5, 1, lbluser);
			  
			  Float mean = new Float(perform.get(3).toString());
			  Label lblmean = new Label();
			  lblmean.setText(mean.toString()+" horas");
			  lblmean.setStyleName("label");
			  grid.setWidget(6, 1, lblmean);
		  }
		  
		  /**
		   * Failure handler
		   */
		  public void onFailure(Throwable throwable) {
			  System.out.println("FALHOU!");
			  throwable.printStackTrace();
		  }
		  
		}
}
